package com.hk.service.impl;

import com.hk.dao.INewsTypeDao;
import com.hk.dao.impl.NewsTypeDaoImpl;
import com.hk.model.NewsType;
import com.hk.service.INewsTypeService;
import com.hk.utils.JdbcMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/19 17:14
 * State：新闻类型业务逻辑层
 */
public class NewsTypeServiceImpl implements INewsTypeService {

    INewsTypeDao iNewsTypeDao = new NewsTypeDaoImpl();

    @Override
    public List<NewsType> findAll(Integer page, Integer limit) {
        ResultSet resultSet = iNewsTypeDao.findAll(page, limit);
        List<NewsType> newsTypeList = transverter(resultSet);
//        处理新闻类型
        if (newsTypeList.isEmpty()) {
            System.out.println("暂时没有新闻类型！");
            newsTypeList = new ArrayList<>();
            return newsTypeList;
        }
//        存储父节点新闻类型菜单
        ArrayList<NewsType> newsTypeArrayList = new ArrayList<>();
//        0:根节点
        for (NewsType newsType : newsTypeList) {
            if (newsType.getParentId() == 0) {
                newsTypeArrayList.add(newsType);
            }
        }
//        查询子节点
        for (NewsType newsType : newsTypeArrayList) {
            //        存储子级类型菜单
            List<NewsType> newsTypeList2 = getChild(newsType.getId(),newsTypeList);
            newsType.setNewsTypeList(newsTypeList2);
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return newsTypeArrayList;
    }

    /**
     * 递归查询子节点
     * @param id 父级Id
     * @param newsTypeList
     * @return
     */
    public List<NewsType> getChild(int id,List<NewsType> newsTypeList) {
        ArrayList<NewsType> childList = new ArrayList<>();
        //      匹配父节点
        for (NewsType newsType : newsTypeList) {
//            匹配成功
            if (id == newsType.getParentId()) {
                childList.add(newsType);
            }
        }

        for (NewsType newsType : childList) {
            List<NewsType> typeList = getChild(newsType.getId(), newsTypeList);
            newsType.setNewsTypeList(typeList);
        }
//        当前节点没有子节点，退出
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    @Override
    public int count(Integer page, Integer limit) {
        ResultSet resultSet = iNewsTypeDao.count(page,limit);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    @Override
    public NewsType findById(Integer id) {
        ResultSet resultSet = iNewsTypeDao.findById(id);
        List<NewsType> newsTypeList = transverter(resultSet);
//        释放资源
        JdbcMysql.jdbcClose();
        return newsTypeList.get(0);
    }

    @Override
    public int save(NewsType t) {
        return 0;
    }

    @Override
    public int update(NewsType t) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    /**
     * 将传递的数据进行转换成集合
     * @param resultSet
     * @return
     */
    public List<NewsType> transverter(ResultSet resultSet ) {
        List<NewsType> newsTypeList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int parentId = resultSet.getInt("parent_id");
                String gmtCreate = resultSet.getString("gmt_create");
                String gmtModified = resultSet.getString("gmt_modified");
//                封装对象
                NewsType newsType = new NewsType(id,name,parentId,gmtCreate,gmtModified);
                newsTypeList.add(newsType);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return newsTypeList;

    }

}
